import {api} from '../../utils/config.js';
const QQMapWX = require('../../static/qqmap-wx-jssdk.min.js')
let qqmapsdk
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fuwu:'定位服务未开启',
    openCityList:[],
    closeCityList:[],
    gpsCity: '定位中',
    cityshow: true
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this;
    let url = api + "/api/fg/cityList";
    app.httpClient.post(url).then(res => {
      let obj  = res.data.obj;
      let cityKtList = obj.cityKtList;
      let cityWktList = obj.cityWktList;
      that.setData({
        openCityList: cityKtList,
        closeCityList:cityWktList
      })
    })
  //请求定位
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({ fuwu: '定位服务已开启' })
        var latitude = res.latitude
        var longitude = res.longitude
        //城市获取
        qqmapsdk = new QQMapWX({
          key: 'S6LBZ-CIK3F-OZEJC-NHGDH-USVZ2-54FYI'
        })
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            // console.log(res);
            let gpsCity = res.result.address_component.city
            gpsCity = gpsCity.substr(0, gpsCity.length - 1)
            let newArr = that.data.openCityList.filter((item) => {
              return item.cityName == gpsCity
            })
            if (newArr.length>0) {
              that.data.openCityList.forEach((item) => {
                if (item.cityName.indexOf(gpsCity) != -1) {
                  that.setData({ gpsCity: gpsCity, areaId: item.cityAreaId, cityName: item.cityName})
                }
              })
            }
            else {
              that.setData({ fuwu: '当前定位城市未开通', cityshow: false })
            }
          },
          fail: function (res) {
            // console.log(res);
          },
          complete: function (res) {
            // console.log(res);
          }
        })
      },
      fail: function () {
        errmessage: '定位失败'
        that.setData({ fuwu: '定位失败', cityshow:false})
      }
    })
  },
  goCity: function() {
    wx.setStorage({
      key: 'cityInfo',
      data: { areaId: this.data.areaId, cityName: this.data.cityName}
    })
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  checkCity: function(e) {
    console.log(e)
    wx.setStorage({
      key: 'cityInfo',
      data: { areaId: e.currentTarget.dataset.storage.cityAreaId, cityName: e.currentTarget.dataset.storage.cityName }
    })
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})